package com.kaishengit.web;


import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.dao.TDao;
import com.kaishengit.entity.Task;

public class EditTaskServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	public void destroy() {
	}
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		HttpSession session=request.getSession();
		String edittaskstate=(String)session.getAttribute("edittaskstate");
		if("edittask".equals(edittaskstate)){
			String[] tstate=request.getParameterValues("tstate");
			String progress=request.getParameter("progress");
			
			String nowtaskid=(String)session.getAttribute("nowtaskid");
			TDao tdao=new TDao();
			Task task=tdao.findById(nowtaskid);
			task.setState(tstate[0]);
			double pro=0.0;
			try{
				System.out.println(pro);
				pro=Double.parseDouble(progress);
				System.out.println(pro);
			}catch(NumberFormatException e){
				e.printStackTrace();
			}
			task.setProgress(pro);
			tdao.update(task);
			String nowgoalid=task.getGoalid();
			session.setAttribute("nowgoalid",nowgoalid);
			//response.sendRedirect("tran.jspx?name=goal&state=addtask");
			response.sendRedirect("tran.jspx?name=mytask");
		}else{
			String tname=request.getParameter("tname");
			String tdescripe=request.getParameter("tdescripe");
			String tendtime=request.getParameter("tendtime");
			String[] tlevel=request.getParameterValues("tlevel");
			
			String nowtaskid=(String)session.getAttribute("nowtaskid");
			TDao tdao=new TDao();
			Task task=tdao.findById(nowtaskid);
			task.setTname(tname);
			task.setEndtime(tendtime);
			task.setTdescripe(tdescripe);
			task.setLevel(tlevel[0]);
			tdao.update(task);
			String nowgoalid=task.getGoalid();
			session.setAttribute("nowgoalid",nowgoalid);
			//response.sendRedirect("tran.jspx?name=goal&state=addtask");
			response.sendRedirect("tran.jspx?name=goal&state=addtask");
		}
	}
	
	public void init() throws ServletException {
	}

}

